package com.yc.fav.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ly.fav.util.MBHelper;
import com.yc.fav.entity.User;
import com.yc.fav.dao.UserMapper;
import com.yc.fav.service.BizException;
import com.yc.fav.service.IUserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ly
 * @since 2024-10-28
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements IUserService {

    @Override
    public User login(String username, String password) throws BizException {
        if(username == null || username.isBlank()){
            throw new BizException("请输入用户名!", -2);
        }
        if(password == null || password.isBlank()){
            throw new BizException("请输入密码!", -3);
        }
        if(password.length() < 3){
            throw new BizException("密码不能少于3位!", -4);
        }
        // 请他验证 , 请自行扩展
        User user = MBHelper.execute(session -> {
            UserMapper mapper = session.getMapper(UserMapper.class);
            LambdaQueryWrapper<User> lqw = new LambdaQueryWrapper<>();
            lqw.eq(User::getName, username);
            lqw.eq(User::getPwd, password);
            return mapper.selectOne(lqw);
        });
        if(user == null){
            throw new BizException("用户名或密码错误!", -1);
        }
        return user;
    }
}
